package com.iotproject.forumservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.forumservice.entity.ForumQuestion;
import com.iotproject.forumservice.entity.vo.QuestionVo;
import com.iotproject.forumservice.service.ForumQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 论坛问题 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-09
 */
@RestController
@RequestMapping("/forumservice/forum-question")
public class ForumQuestionController {

    @Autowired
    ForumQuestionService forumQuestionService;

    //问题列表显示
    /**
     * 根据页码和大小分页查询
     * @param page 当前页，默认为1
     * @param size 当前每页显示行数，默认为5
     * @return 页信息的实体
     */
    @GetMapping("/questionList/{page}/{size}")
    public R getAllListByPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> maps = forumQuestionService.getAllListByPage(page, size);
        return R.ok().data("maps", maps);
    }

    //根据话题ID获取话题信息
    @GetMapping("getQuestionById/{questionId}")
    public R getQuestionById(@PathVariable("questionId") String questionId) {
        QuestionVo question = forumQuestionService.getQuestionById(questionId);
        if (question == null) {
            return R.error();
        }
        return R.ok().data("question", question);
    }

    //获取话题总评论数(在评论区成功回复之后调用)
    @GetMapping("getCommentCount/{questionId}")
    public R getCommentCount(@PathVariable("questionId") String questionId) {
        ForumQuestion question = forumQuestionService.getById(questionId);
        if (question == null) {
            return R.error().message("话题不存在");
        }
        return R.ok().data("commentCount", question.getCommentCount());
    }

    //根据标签获取相关话题(这里使用正则表达式，由于question.tag已经使用 | 分割，所以直接使用即可)
    @GetMapping("relatedQuestion/{questionId}/{tags}")
    public R relatedQuestion(@PathVariable("questionId") String questionId, @PathVariable("tags") String tags) {
        List<ForumQuestion> relatedQuestions = forumQuestionService.relatedQuestion(questionId, tags);
        return R.ok().data("relatedQuestions", relatedQuestions);
    }

    //根据condition条件获取热门话题
    @GetMapping("hotTopic/{condition}")
    public R hotTopic(@PathVariable("condition") String condition) {
        List<ForumQuestion> hotTopicList = forumQuestionService.hotTopic(condition);
        return R.ok().data("hotTopicList", hotTopicList);
    }

    @GetMapping("/questionListByKeyWords/{keyWords}/{page}/{size}")
    public R questionListByKeyWords(@PathVariable("keyWords") String keyWords, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> maps = forumQuestionService.questionListByKeyWords(keyWords, page, size);
        return R.ok().data("maps", maps);
    }

    //根据用户id分页展示所有问题
    @GetMapping("/questionListByUserId/{userId}/{page}/{size}")
    public R questionListByUserId(@PathVariable("userId") String userId, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> maps = forumQuestionService.questionListByUserId(userId, page, size);
        return R.ok().data("maps", maps);
    }

}

